﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;

namespace Project5188.BUS.Operations
{
    public class StudentInfos : DetailView<studentinfo>
    {
        private project5188Entities _context;
        // ReSharper disable InconsistentNaming
        private new List<studentinfo> EntityList;
        private new studentinfo Entity;
        // ReSharper restore InconsistentNaming

        public long TransactionId { get; private set; }

        #region Overrides of DetailView<studentinfo>

        public override List<studentinfo> GetEntityList()
        {
            _context = new project5188Entities();
            EntityList = _context.studentinfoes.Where(a => !a.IsDeleted).OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }

        public override List<studentinfo> GetEntityById(long id)
        {
            _context = new project5188Entities();
            EntityList = _context.studentinfoes.Where(a => a.a_ID == id && !a.IsDeleted).OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }

        public override studentinfo GetEntity()
        {
            _context = new project5188Entities();
            Entity = new studentinfo();
            return Entity;
        }

        public override void Update()
        {
            _context.SaveChanges();
        }

        public override void Insert()
        {
            _context.AddTostudentinfoes(Entity);
            _context.SaveChanges();
            TransactionId = Entity.a_ID;
        }

        #endregion

        public List<studentinfo> GetEntityListByParameters(string tcNoText, string idCardSerialNoSequence, string orderNoText, string nameSurname, long genderId, bool? isExaminationFeePaid, bool? isSuccessfully, string certificateNoText)
        {
            _context = new project5188Entities();
            var parameterList = new List<ObjectParameter>();

            var dynamicString = "it.IsDeleted = @IsDeleted";
            parameterList.Add(new ObjectParameter("IsDeleted", false));

            if (!String.IsNullOrEmpty(tcNoText) && !String.IsNullOrWhiteSpace(tcNoText))
            {
                dynamicString += " And it.b_TCNo = @TCNo";
                parameterList.Add(new ObjectParameter("TCNo", Convert.ToInt32(tcNoText)));
            }

            if (!String.IsNullOrEmpty(idCardSerialNoSequence) && !String.IsNullOrWhiteSpace(idCardSerialNoSequence))
            {
                dynamicString += " And it.c_IDCardSerialNoSequence = @IDCardSerialNoSequence";
                parameterList.Add(new ObjectParameter("IDCardSerialNoSequence", idCardSerialNoSequence));
            }

            if (!String.IsNullOrEmpty(orderNoText) && !String.IsNullOrWhiteSpace(orderNoText))
            {
                dynamicString += " And it.r_OrderNo = @OrderNo";
                parameterList.Add(new ObjectParameter("OrderNo", Convert.ToInt32(orderNoText)));
            }

            if (!String.IsNullOrEmpty(nameSurname) && !String.IsNullOrWhiteSpace(nameSurname))
            {
                dynamicString += " And it.d_NameSurname = @NameSurname";
                parameterList.Add(new ObjectParameter("NameSurname", nameSurname));
            }

            if (genderId > 0)
            {
                dynamicString += " And it.g_GenderParameterValueID_4 = @GenderId";
                parameterList.Add(new ObjectParameter("GenderId", genderId));
            }

            if (isExaminationFeePaid != null)
            {
                dynamicString += " And it.zt_IsExaminationFeePaid = @IsExaminationFeePaid";
                parameterList.Add(new ObjectParameter("IsExaminationFeePaid", isExaminationFeePaid));
            }

            if (isSuccessfully != null)
            {
                dynamicString += " And it.zz_IsSuccessfully = @IsSuccessfully";
                parameterList.Add(new ObjectParameter("IsSuccessfully", isSuccessfully));
            }

            if (!String.IsNullOrEmpty(certificateNoText) && !String.IsNullOrWhiteSpace(certificateNoText))
            {
                dynamicString += " And it.zzc_CertificateNo = @CertificateNo";
                parameterList.Add(new ObjectParameter("CertificateNo", Convert.ToInt32(certificateNoText)));
            }

            EntityList = _context.studentinfoes.Where(dynamicString, parameterList.ToArray()).
                OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }
    }
}
